package com.example.dllo.keepdemo.train.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.base.BaseActivity;
import com.example.dllo.keepdemo.tools.GsonRequest;
import com.example.dllo.keepdemo.tools.MyListView;
import com.example.dllo.keepdemo.tools.VolleySingleton;
import com.example.dllo.keepdemo.train.adapter.TrainCourseDetailPlanAdapter;
import com.example.dllo.keepdemo.train.adapter.TrainCourseDetailWorkoutsAdapter;
import com.example.dllo.keepdemo.train.bean.TrainCourseDetailBean;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 16/1/25.
 */
public class TrainCourseDetailActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    private Toolbar toolbar;
    private ScrollView scrollView;
    private MyListView listViewPlan;
    private MyListView listViewWorkouts;
    private TextView tvTitle;
    private TrainCourseDetailPlanAdapter planAdapter;
    private TrainCourseDetailWorkoutsAdapter workoutsAdapter;
    private TrainCourseDetailBean trainCourseDetailBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_traincoursedetail);
        toolbar = (Toolbar) findViewById(R.id.toolbar_activity_traincoursedetail);
        scrollView = (ScrollView) findViewById(R.id.scroll_activity_traincoursedetail);
        listViewPlan = (MyListView) findViewById(R.id.listview_activity_traincoursedetail_plan);
        listViewWorkouts = (MyListView) findViewById(R.id.listview_activity_traincoursedetail_workouts);
        tvTitle = (TextView) findViewById(R.id.tv_activity_traincoursedetail_title);

        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.actionlastbtn);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TrainCourseDetailActivity.this.finish();
            }
        });

        initData();
        listViewPlan.setOnItemClickListener(this);
        listViewWorkouts.setOnItemClickListener(this);
        scrollView.smoothScrollTo(0,0);

    }

    private void initData() {
        trainCourseDetailBean = new TrainCourseDetailBean();
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        String title = intent.getStringExtra("title");
        tvTitle.setText(title);
        String url = "https://api.gotokeep.com/v1.1/workouthashtag/" + id;
        GsonRequest<TrainCourseDetailBean> gsonRequest = new GsonRequest<TrainCourseDetailBean>(url, TrainCourseDetailBean.class, new Response.Listener<TrainCourseDetailBean>() {
            @Override
            public void onResponse(TrainCourseDetailBean response) {
                trainCourseDetailBean = response;
                planAdapter = new TrainCourseDetailPlanAdapter(TrainCourseDetailActivity.this,response);
                workoutsAdapter = new TrainCourseDetailWorkoutsAdapter(TrainCourseDetailActivity.this,response);
                listViewPlan.setAdapter(planAdapter);
                listViewWorkouts.setAdapter(workoutsAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String,String> map = new HashMap<>();
                map.put("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NjY1MDBlMTYzZGIwYzlmNTdiMTQwYWQiLCJ1c2VybmFtZSI6Iuaeq-WQmyIsImF2YXRhciI6Imh0dHA6Ly93eC5xbG9nby5jbi9tbW9wZW4vc2t3MmhSR3lXRWcwUzV0NzhkMlRVUnFhYjBzcWJ4RXB4S1BLUFpEOXpwbTNJbkIySzRjRnhLVEJsemdDb3VIcGlia3RkRUluQ0NFcGpIS2FHZ1lwRkp6SVRIVU5NYTNpYmYvMCIsImlhdCI6MTQ1MjU4OTkwNiwiZXhwIjoxNDU1MTgxOTA2LCJpc3MiOiJodHRwOi8vd3d3LmdvdG9rZWVwLmNvbS8ifQ.4I6FpbJT1_1fe8Z51oHt6Mv5t-3zXGGYhI1rMe3U8gM");
                map.put("X-X", "486bba2566a02d3ae31ed4445366a873b89cf910");
                map.put("Host", "api.gotokeep.com");
                map.put("X-KEEP-VERSION", "2.8.1");
                map.put("X-KEEP-CHANNEL", "keep");
                return map;
            }
        };
        VolleySingleton.getInstance()._addRequest(gsonRequest);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (id == 1) { //plan
            Intent intent = new Intent(TrainCourseDetailActivity.this,TrainPlanActivity.class);
            intent.putExtra("id",trainCourseDetailBean.getData().getResults().getPlans().get(position).get_id());
            startActivity(intent);
        } else if (id == 2) {  //workouts
            Intent intent = new Intent(TrainCourseDetailActivity.this,TrainWorkoutsActivity.class);
            intent.putExtra("id",trainCourseDetailBean.getData().getResults().getWorkouts().get(position).get_id());
            startActivity(intent);
        }
    }
}
